package com.tool.finances.lifestyle;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.method.DialerKeyListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.tool.finances.lifestyle.data.AccountProvider;
import com.tool.finances.lifestyle.data.AccountType;
import com.waps.AppConnect;
import com.waps.util.RateUSModel;
import com.waps.util.ShowLicenseActivity;

public class AccountActivity extends Activity {

	private String dateString;
	private String categoryString;
	private String commentString;
	private SimpleDateFormat dateFormat;
	private int account_Tag = 0;

	// TextView textTitle;
	RadioButton incomeRadio;
	RadioButton outcomeRadio;
	Spinner categoryText;
	EditText moneyText;
	ImageView imageDate;
	TextView textDate;
	EditText commentText;
	// ImageView imageAdd;
	ImageView imageCancel;
	ImageView imageOk;

	private Cursor cursor = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account);
		
		if (RateUSModel.getInstance(AccountActivity.this).isShouldShowLicense(3)) {
			ShowLicenseActivity.showLicenseDialog(this);
		}else {
			RateUSModel.getInstance(this).showMoreApps(true, 10);
		}
		RateUSModel.getInstance(AccountActivity.this).initUserPoint();
		
		
		
		getView();

		dateFormat = new SimpleDateFormat("yyyy/M/dd");
		dateString = dateFormat.format(new Date());
		textDate.setText(dateString);

		imageDate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					final Date date = dateFormat.parse(textDate.getText().toString());

					new DatePickerDialog(AccountActivity.this,
							new OnDateSetListener() {

								@Override
								public void onDateSet(DatePicker view,
										int year, int monthOfYear,
										int dayOfMonth) {
									final String datetext = String.valueOf(year)
											+ "/"
											+ String.valueOf(monthOfYear + 1)
											+ "/" + String.valueOf(dayOfMonth);
									textDate.setText(datetext);
								}
							}, date.getYear() + 1900, date.getMonth(), date
									.getDate()).show();
				} catch (final ParseException e) {

					e.printStackTrace();
				}
			}
		});

		imageCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new AlertDialog.Builder(AccountActivity.this)
						.setTitle(getResources().getString(R.string.app_name))
						.setMessage(getResources().getString(R.string.exit_app))
						.setPositiveButton(
								getResources()
										.getString(R.string.dialog_cancel),
								null).setNegativeButton(
								getResources().getString(
										R.string.dialog_confrim),
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										finish();
									}
								}).show();
			}
		});
		imageOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (incomeRadio.isChecked()) {
					account_Tag = FinancesTabActivity.INCOME_TAG;
				}
				if (outcomeRadio.isChecked()) {
					account_Tag = FinancesTabActivity.OUTCOME_TAG;
				}

				categoryString = categoryText.getSelectedItem().toString();
				try {
					Float.parseFloat(moneyText.getText().toString());
				} catch (final Exception e) {

					new AlertDialog.Builder(AccountActivity.this).setMessage(
							"Please input number").show();
					return;
				}
				commentString = commentText.getText().toString();

				final ContentValues values = new ContentValues();
				values.put(AccountType.Account.ACCOUNTDATE, textDate.getText()
						.toString());
				values.put(AccountType.Account.ACCOUNTTYPE, categoryString);

				values.put(AccountType.Account.MONEYTYPE, "RMB");
				values.put(AccountType.Account.COMMENT, commentString);

				switch (account_Tag) {
				case FinancesTabActivity.INCOME_TAG:
					values.put(AccountType.Account.INOROUT, "True");
					values.put(AccountType.Account.ACCOUNTMONEY, moneyText
							.getText().toString());
					break;
				case FinancesTabActivity.OUTCOME_TAG:
					values.put(AccountType.Account.INOROUT, "False");
					values.put(AccountType.Account.ACCOUNTMONEY, "-"
							+ moneyText.getText().toString());
					break;
				default:
					break;
				}

				Uri uri = null;
				try {
					uri = getContentResolver().insert(
							AccountProvider.ACCOUNT_CONTENT_URI, values);
				} catch (final Exception e) {
				}
				if (null != uri) {
					Toast.makeText(AccountActivity.this, "添加成功",
							1000).show();
					commentText.setText("");
					moneyText.setText("");
				}
				// finish();
			}
		});
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		AppConnect.getInstance(this).finalize();
	}

	@Override
	protected void onResume() {
		cursor = getContentResolver().query(
				AccountProvider.CATEGORY_CONTENT_URI,
				AccountType.Account.CATEGORYPROECTION, null, null, null);
		final String[] group = getStringGroup(cursor);
		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, group);
		// adapter
		categoryText.setAdapter(adapter);

		super.onResume();
	}

	private String[] getStringGroup(Cursor cursor) {
		final String[] group = new String[cursor.getCount()];
		int i = 0;
		if (cursor.moveToFirst()) {
			do {
				final int index = cursor
						.getColumnIndex(AccountType.Account.CATEGORYTYPE);
				group[i] = cursor.getString(index);
				i++;
			} while (cursor.moveToNext());
		}
		return group;
	}

	private void getView() {
		// textTitle = (TextView) findViewById(R.id.TextTitle);
		categoryText = (Spinner) findViewById(R.id.CategorySpinner);
		moneyText = (EditText) findViewById(R.id.EditTextMoney);
		incomeRadio = (RadioButton) findViewById(R.id.IncomeRadio);
		incomeRadio.setChecked(true);
		outcomeRadio = (RadioButton) findViewById(R.id.OutcomeRadio);
		outcomeRadio.setChecked(false);

		moneyText.setKeyListener(new DialerKeyListener());
		textDate = (TextView) findViewById(R.id.TextDate);
		imageDate = (ImageView) findViewById(R.id.ImageDate);
		commentText = (EditText) findViewById(R.id.CommentText);

		imageCancel = (ImageView) findViewById(R.id.ImageCancel);
		imageOk = (ImageView) findViewById(R.id.ImageOk);
	}

}
